﻿using System;
using System.Linq;
using System.Web.Script.Serialization;
using Microsoft.VisualBasic.FileIO;
using System.Text.RegularExpressions;

namespace EAPtoHTML
{
    static class UtilityExtensions
    {
        public static string toJSON(this Object o)
        {
                var jss = new JavaScriptSerializer();
                return jss.Serialize(o);
        }
        public static string toJSONforDataTablesDotNet(this Object o)
        {
            return @"{""aaData"": " + o.toJSON() + "}";
        }
        //custom helper function to consolidate some keywords and return them delimited by something provided
        public static string ConsolidateKeywords(this string StringToCheck, string StringDelimiter)
        {

            // remove all html if found (we don't need it but sometimes it is a spacer between words)
            StringToCheck = Regex.Replace(StringToCheck, @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", " ");

            //remove non alpha-numeric
            StringToCheck = Regex.Replace(StringToCheck, "[^a-zA-Z0-9]+", " ");

            //consolidate spaces
            StringToCheck = Regex.Replace(StringToCheck, @"\s+", " ");

            //get a distinct list of all, upper case, words that are longer than 2 characters
            var AllUniqueWords = StringToCheck.ToUpper().Split(' ').Where(stc => stc.Length > 2).Distinct();

            //collapse text words into themselves (count, counter, countered will all collapse into countered)
            var CollapsedWords =
                from UniqueWord in AllUniqueWords
                let BiggestMatchingWord = AllUniqueWords.OrderByDescending(auw => auw.Length).Where(f => f.Contains(UniqueWord)).FirstOrDefault()
                select BiggestMatchingWord.Equals(null) ? UniqueWord : BiggestMatchingWord;

            //return the unique list of collapsed words, alpha sorted
            return string.Join(StringDelimiter, CollapsedWords.Distinct().OrderBy(cw => cw).ToArray());
        }

        public static void CopyThisFolderTo(this string SourcePath,string TargetPath)
        {
            FileSystem.CopyDirectory(SourcePath, TargetPath);
        }
    }
}
